import { DataSet } from 'choerodon-ui/pro';
import { apiTestApiConfig } from '@choerodon/master';
import { relativeObjData } from './constants';

const Index = ({
  projectId,
  useStore,
  level,
}: any): any => ({
  autoCreate: true,
  fields: [{
    name: 'taskType',
    type: 'string',
    label: '关联对象',
    defaultValue: relativeObjData[0].value,
    textField: 'name',
    valueField: 'value',
    options: new DataSet({
      data: relativeObjData,
    }),
    disabled: level !== 'project' || window.location.href.includes('project-pipeline-template'),
  }, {
    name: 'apiTestTaskId',
    type: 'string',
    label: 'API测试任务',
    textField: 'name',
    valueField: 'id',
    disabled: level !== 'project' || window.location.href.includes('project-pipeline-template'),
    dynamicProps: {
      required: ({ record }: any) => record?.get('taskType') === relativeObjData[0].value
      && level === 'project' && !window.location.href.includes('project-pipeline-template'),
    },
    options: new DataSet({
      // 这里待整理逻辑
    //   autoQuery: !hostGroupDeploy,
      autoQuery: true,
      paging: true,
      transport: {
        read: ({ data }): any => {
          const { id } = data;
          return projectId ? ({
            method: 'get',
            // 这里取消到了random
            // url: `/test/v1/projects/${projectId}/api_test/tasks/\
            // paging?random=${random}${id ? `&id=${id}` : ''}`,
            url: `/test/v1/projects/${projectId}/api_test/tasks/paging?${id ? `id=${id}` : ''}`,
            transformResponse: (res: any) => {
              let newRes = res;
              try {
                newRes = JSON.parse(res);
                useStore.setApiTestArray(newRes.content);
                return newRes;
              } catch (e) {
                return newRes;
              }
            },
          }) : undefined;
        },
      },
    }),
  }, {
    name: 'apiTestConfigId',
    type: 'string',
    label: '任务配置',
    textField: 'name',
    disabled: level !== 'project' || window.location.href.includes('project-pipeline-template'),
    valueField: 'id',
    options: new DataSet({
      autoQuery: true,
      //   autoQuery: !hostGroupDeploy,
      transport: {
        read: () => (projectId ? ({
          ...apiTestApiConfig.getConfigList(),
          params: undefined,
        }) : undefined),
      },
    }),
  }, {
    // 这个字段只是前端用来判断是否显示的
    name: 'whetherDelay',
    type: 'boolean',
    label: '是否开启延迟执行',
    disabled: level !== 'project' || window.location.href.includes('project-pipeline-template'),
    defaultValue: false,
    textField: 'text',
    valueField: 'value',
    options: new DataSet({
      data: [{
        value: true,
        text: '是',
      }, {
        value: false,
        text: '否',
      }],
    }),
  }, {
    name: 'startIn',
    type: 'string',
    label: '延迟时间',
    disabled: level !== 'project' || window.location.href.includes('project-pipeline-template'),
    dynamicProps: {
      required: ({ record }: any) => record?.get('whetherDelay'),
    },
  }, {
    name: 'apiTestSuiteId',
    type: 'string',
    label: 'API测试套件',
    textField: 'name',
    valueField: 'id',
    dynamicProps: {
      required: ({ record }: any) => record?.get('taskType') === relativeObjData[1].value,
    },
    options: new DataSet({
      // autoQuery: !hostGroupDeploy,
      autoQuery: true,
      transport: {
        read: () => (projectId ? ({
          ...apiTestApiConfig.getSuitesList(),
        }) : undefined),
      },
    }),
  }, {
    name: 'enableWarningSetting',
    type: 'boolean',
    label: '是否启用告警设置',
    disabled: level !== 'project' || window.location.href.includes('project-pipeline-template'),
    defaultValue: false,
    textField: 'text',
    valueField: 'value',
    options: new DataSet({
      data: [{
        value: true,
        text: '是',
      }, {
        value: false,
        text: '否',
      }],
    }),
  }, {
    name: 'performThreshold',
    type: 'string',
    label: '执行阈值',
    dynamicProps: {
      required: ({ record }: any) => record?.get('enableWarningSetting') && (level === 'project' && !window.location.href.includes('project-pipeline-template')),
      disabled: ({ record }: any) => !record?.get('enableWarningSetting') || (level !== 'project' || window.location.href.includes('project-pipeline-template')),
    },
  }, {
    name: 'notifyUserIds',
    type: 'string',
    label: '通知对象',
    dynamicProps: {
      required: ({ record }: any) => record?.get('enableWarningSetting') && (level === 'project' && !window.location.href.includes('project-pipeline-template')),
      disabled: ({ record }: any) => !record?.get('enableWarningSetting') || (level !== 'project' || window.location.href.includes('project-pipeline-template')),
    },
    textField: 'realName',
    valueField: 'id',
    multiple: true,
    lookupAxiosConfig: projectId ? {
      url: `/cbase/choerodon/v1/projects/${projectId}/users?page=0&size=20`,
      method: 'get',
    } : undefined,
  }, {
    name: 'notifyWay',
    type: 'string',
    label: '通知方式',
    multiple: ',',
    dynamicProps: {
      disabled: ({ record }: any) => !record?.get('enableWarningSetting') || (level !== 'project' || window.location.href.includes('project-pipeline-template')),
    },
    textField: 'text',
    valueField: 'value',
    options: new DataSet({
      data: [{
        value: 'sendEmail',
        text: '邮件',
      }, {
        value: 'sendSiteMessage',
        text: '站内信',
      }],
    }),
  }, {
    name: 'blockAfterJob',
    type: 'boolean',
    label: '执行成功率低于阈值后是否阻塞',
    defaultValue: true,
    textField: 'text',
    dynamicProps: {
      disabled: ({ record }: any) => (level !== 'project' || window.location.href.includes('project-pipeline-template')),
    },
    valueField: 'value',
    options: new DataSet({
      data: [{
        value: true,
        text: '是',
      }, {
        value: false,
        text: '否',
      }],
    }),
  }],
  events: {
    update: ({ name, value, record }: any) => {
      switch (name) {
        case 'whetherDelay': {
          if (!value) {
            record.set('startIn', undefined);
          }
          break;
        }
        default: {
          break;
        }
      }
    },
  },
});

export default Index;
